<template>
	<z-paging ref="paging" v-model="indexList" @query="queryList" :show-loading-more-no-more-view="false" :loading-more-enabled	= "false">
		<template slot="top">
			 <!-- rightIcon='/static/icon/gengduo.svg'  @rightClick="goFeature" -->
			<cl-navbar autoBack title="" bgColor="transparent" :isbgColor="false" leftIconColor="#ffffff">
			</cl-navbar>
			<view
				style="width: 1000rpx;height: 550rpx;background-color: #FF6052;border-radius: 50%;margin-left: -125rpx;margin-top: -300rpx;">
			</view>
			<view class="redInfoView">
				<view class="infoview" v-if="chatInfo.chat_type == 'private'">
					<cl-avatar
						:src="redInfo.sender_id == user_info.user_id ? user_info && user_info.avatar_url : chatInfo && chatInfo.avatar_url"
						size="60rpx"></cl-avatar>
					<text class="namered">{{ $t('redPacket.display.redEnvelope', { name: redInfo.sender_id == user_info.user_id ? user_info.nick_name : chatInfo.nickname }) }}</text>
				</view>
				<view class="infoview" v-if="chatInfo.chat_type == 'group'">
					<cl-avatar
						:src="redInfo.sender_id == user_info.user_id ? user_info && user_info.avatar_url : getUserInfo(redInfo.sender_id).avatar_url"
						size="60rpx"></cl-avatar>
					<text class="namered">{{ $t('redPacket.display.redEnvelope', { name: redInfo.sender_id == user_info.user_id ? user_info.nick_name : getUserInfo(redInfo.sender_id).group_nickname || getUserInfo(redInfo.sender_id).user_nickname || '' }) }}</text>
				</view>
				<text class="reddetails">{{redInfo.remark}}</text>
				<template v-if="getInfo">
					<view class="moneyView">
						<text class="moneyValue">{{getInfo.amount}}</text>
						<text class="moneyUnit">{{ $t('redPacket.display.yuan') }}</text>
					</view>
					<text style="font-size: 28rpx;color: #FC8F2C;margin-top: 20rpx;">{{ $t('redPacket.display.savedToBalance') }}</text>
				</template>
				<view style="margin-top: 80rpx;color: #595A66;font-size: 26rpx;align-self: flex-start;" v-if="shengyuInfo && chatInfo.chat_type == 'group'">{{ $t('redPacket.display.received', { received: shengyuInfo.quantity - shengyuInfo.remaining_count, total: shengyuInfo.quantity }) }}</view>
				<view style="margin-top: 80rpx;color: #595A66;font-size: 26rpx;align-self: flex-start;" v-if="noReceive">{{ $t('redPacket.display.amount', { amount: redInfo.amount }) }}{{ $t('redPacket.display.yuan') }}，{{redInfo.red_status == '1' ? $t('redPacket.display.expired') : $t('redPacket.display.waitingForRecipient') }}</view>
			</view>
		</template>

		<view :key="index" v-for="(item, index) in indexList">
			<view class="listitem">
				<view class="listitem__centent">
					<view class="listitem__centent__left">
						<cl-avatar
							:src="item.user.avatar_url"
							size="100rpx"></cl-avatar>
					</view>
					<view class="listitem__centent__right">
						<text class="listitem__centent__right__nickname">{{item.user.nick_name}}</text>
						<view class="listitem__centent__right__content">
							{{item.create_time|formatTime}}
						</view>
					</view>
				</view>
				<view class="listitem__controls">
					<text>{{item.amount}}{{ $t('redPacket.display.yuan') }}</text>
					<text v-if="item.best" style="color: #FC8F2C;font-size: 26rpx;">{{$t("redPacket.display.bestLuck")}}</text>
				</view>
			</view>
		</view>
		<view>
			<u-action-sheet :actions="selcetData" closeOnClickOverlay safeAreaInsetBottom @close="selectShow = false"
				lineColor="#ffffff" round="20rpx" :show="selectShow" :cancelText="$t('common.cancelText')"
				@select="selectShowClick"></u-action-sheet>
		</view>
	</z-paging>
</template>

<script>
	import $T from '@/common/tools/time.js';
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				indexList: [],
				getInfo:null,
				redInfo:null,
				chatInfo:null,
				noReceive:false,
				shengyuInfo:null,
				
				selcetData: [{
					name: this.$t("redPacket.display.redPacketRecord"),
					value: 'redPacketRecord'
				}],
				selectShow: false,
			};
		},
		filters: {
			formatTime(value) {
				if (!value) return ''
				return $T.gettime(value);
			},
		},
		computed: {
			...mapState({
				user_info: state => state.user.user_info,
				groupMemberInfo: state => state.groupMembersInfo.groupMemberInfo,
			}),
			getUserInfo(){
				return (uid) => {
					let useitem = this.groupMemberInfo[uid]
					if (useitem) {
						return useitem
					}
					return {}
				}
			},
		},
		onLoad() {
			let query = this.$Route.query
			this.getInfo = query.getinfo || null
			this.redInfo = query.redinfo
			this.chatInfo = query.chatInfo
			console.log(this.getInfo);
			console.log(this.redInfo);
			console.log(this.chatInfo);
			if(this.chatInfo.chat_type == 'private' && this.redInfo.remaining_count > 0 && this.redInfo.sender_id == this.user_info.user_id){
				this.noReceive = true //未领取
			}
		},
		methods: {
			selectShowClick(e) {
				console.log(e);
				this.$Router.push({
					name:e.value
				})
			},
			goFeature(){
				this.selectShow = true
			},
			async queryList(pageNo, pageSize) {
				if(this.noReceive)return
				try{
					let res = await uni.$api.chat.redPacketList({
						msg_id:this.redInfo.msg_id,
						chat_type:this.redInfo.chat_type,
						red_id:this.redInfo.id,
						target_id:this.redInfo.target_id,
						page:pageNo,
						limit:pageSize
					})
					if(res && res.code == 200){
						let index = res.data.list.findIndex(item=>item.user_id == this.user_info.user_id)
						if(index != -1){
							this.getInfo = {
								red_id:res.data.list[index].id,
								amount:res.data.list[index].amount
							}
						}
						this.shengyuInfo = res.data.red_info
						if(this.redInfo.packet_type == '1' && res.data.red_info.remaining_count == '0'){
							// 计算最大 amount
							const maxAmount = Math.max(...res.data.list.map(item => parseFloat(item.amount)));
							// 增加 best 字段
							res.data.list.forEach((item,index) => {
							    res.data.list[index].best = parseFloat(item.amount) === maxAmount;
							});
						}
						this.$refs.paging.complete(res.data.list)
					}else if(res && res.code == 2003){
						this.$refs.paging.complete([])
					}else{
						this.$refs.paging.complete(false)
					}
				}catch(e){
					this.$refs.paging.complete(false)
				}
			},
		},
	}
</script>

<style lang="scss">
	.redInfoView {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin: 50rpx 30rpx;

		.infoview {
			display: flex;
			align-items: center;

			.namered {
				font-size: 32rpx;
				font-weight: bold;
				margin-left: 10rpx;
			}
		}

		.reddetails {
			font-size: 26rpx;
			color: #595A66;
			text-align: center;
			margin-top: 20rpx;
		}

		.moneyView {
			display: flex;
			align-items: flex-end;
			margin-top: 50rpx;
			.moneyValue {
			    font-size: 80rpx;
			    color: #FC8F2C;
			    font-weight: 700;
			    line-height: 1; /* 避免文字顶部多余的间距 */
			}
			
			.moneyUnit {
			    font-size: 28rpx;
			    color: #FC8F2C;
			    margin-left: 10rpx; /* 添加间距来美化布局 */
			}
		}
	}

	.listitem {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		position: relative;
		
		&__centent {
			display: flex;
			align-items: center;
			&__right {
				margin-left: 20rpx;
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				&__nickname {
					font-size: 30rpx;
				}

				&__content {
					margin-top: 5rpx;
					font-size: 26rpx;
					color: #595A66;
				}
			}
		}

		&__controls {
			display: flex;
			flex-direction: column;
			font-size: 32rpx;
			color: #595A66;
			font-weight: 500;
			margin-top: 15rpx;
			align-items: flex-end;
		}

	}
</style>